import React, { createContext, useReducer } from 'react';
import { UPDATE_PARAMS,SHOWLOADING,HIDELOADING ,UPDATE_CATENAME} from './actionType';
export const AppContext = createContext();
const reducer = (state, action) => {
  
    const newState = { ...state }
    switch (action.type) { 
        case UPDATE_PARAMS:
            newState.params = {...state.params,...action.data}
            return newState;
        case SHOWLOADING:
            newState.loading = true;
            return newState;
        case HIDELOADING:
            newState.loading = false;
            return newState;
        case UPDATE_CATENAME:
            newState.cateName = action.data;
            return newState
        case SET_ADDRESS:
            newState.addressData = {...state.addressData,...action.data}
        
    }
}
export const AppStore = (props) => {
    const [appData, dispatch] = useReducer(reducer, {params: {pageSize:20,current:1},loading:false,tags:[],cateName:'',addressData:{} });
    return <AppContext.Provider value={{ appData, dispatch }}>
        {props.children}
    </AppContext.Provider>
}